/*
 * 时间设置
 * 舞者无罪
 * 2014.12.01
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "knit_line.h"
#include "com.h"
#include "keypane.h"


#define KNIT_LINE_CAPTION_LEFT       (CX_EDGE_LEFT)
#define KNIT_LINE_CAPTION_TOP        (CY_TITLE4_HEIGHT + CX_EDGE_LEFT * 2)
#define KNIT_LINE_CAPTION_WIDTH      (160)
#define KNIT_LINE_CAPTION_HEIGHT     (28)
#define KNIT_LINE_NAME_WIDTH         (80)
/*
 *
 */
static void CreateKnitLineInterface(HWND hWnd)
{
        CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_TIME_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    char sc[32] = {0};
    CreateWindow (MH_LABEL_NAME,
        MW_INPUT_KNIT_LINE_CAPTION,
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        KNIT_LINE_CAPTION_LEFT,
        KNIT_LINE_CAPTION_TOP,
        KNIT_LINE_CAPTION_WIDTH,
        KNIT_LINE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    sprintf(sc, "%u", GetLineToStart(g_knit_data));
    CreateWindow (MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        ID_EDIT,
        KNIT_LINE_CAPTION_LEFT + KNIT_LINE_CAPTION_WIDTH,
        KNIT_LINE_CAPTION_TOP,
        KNIT_LINE_NAME_WIDTH,
        KNIT_LINE_CAPTION_HEIGHT,
        hWnd, 0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_ESC:
        {
             if(STATE_STOP == GetRunState(g_knit_data))
            {
                HWND hHosting = GetHosting(hWnd);
                ushort line = atoi(GetWindowCaption(GetDlgItem(hWnd, ID_EDIT)));
                SendMessage(hHosting, MSG_GOTO_LINE, line, 0);
                PostMessage(hWnd, MSG_CLOSE, 0, 0);
            }
        }
        break;
    case ID_EDIT:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            HWND hTmp = GetDlgItem(hWnd, nID);
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            ushort max = data.value ? data.value : GetTotalKnitPage(g_knit_data);
            CreateKeyboardWindow(hWnd, rect, 1, max, atoi(GetWindowCaption(hTmp)));
        }
        break;
    default:
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
         if(STATE_STOP == GetRunState(g_knit_data))
        {
            HWND hHosting = GetHosting(hWnd);
            ushort line = atoi(GetWindowCaption(GetDlgItem(hWnd, ID_EDIT)));
            SendMessage(hHosting, MSG_GOTO_LINE, line, 0);
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    else if(key == SCANCODE_ENTER)
    {
       OnBtnCmd(hWnd, ID_EDIT);
    }
}
/*
 *
 */
static int KnitLineWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateKnitLineInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_TIME]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_SETDATA:
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateKnitLineWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_TIME_BMP) / 2;
    int y = (CY_SCREEN - CY_TIME_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = KnitLineWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_TIME_BMP;
    CreateInfo.by = y + CY_TIME_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_EDIT));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
